/**
 * @author flower.cheng
 * @description 公用directives
 */

import Vue from 'vue'
import {
  MessageBox
} from 'mint-ui'
import userAuth from '@/service/userAuth'
import jumpConfig from '../config/jump.conf';
let countdowInstance = null
Vue.directive('auth-click', function (el, binding) {
  let callback = binding.value;
  if (callback && typeof callback === 'function') {
    el.onclick = function () {
      console.log("auth user click", callback);
      if (userAuth.isLogin()) {
        callback.call(this);
      } else {
        MessageBox({
          message: '您还没有登录，请先登录！',
          confirmButtonClass: 'boqii-confirm-btn'
        }).then(action => {
          console.log("我要去登录了哈哈哈", $vueInstance)
          $vueInstance.$router.push({
            path: '/auth/login'
          })

          // callback.call(this);
        });
      }
    }
  }
})

Vue.directive('download-click', function (el, binding) {
  let callback = binding.value;
  // if (callback && typeof callback === 'function') {
  el.onclick = function () {
    console.log("auth user click", callback);
    window.location.href = jumpConfig.downloadUrl;
    // MessageBox({
    //   message: '是否去下载APP',
    //   showCancelButton: true,
    //   confirmButtonClass: 'boqii-confirm-btn'
    // }).then(action => {
    //   if( action == 'confirm'){
    //     window.location.href = jumpConfig.downloadUrl;
    //   }
    //   // $vueInstance.$router.push({
    //   //   path: '/common/download'
    //   // })
    //   // callback.call(this);
    // });
  }
  // }
})

Vue.directive('count-down', {
  inserted(el, binding) {
    let text = el.innerHTML;
    let callback = binding.value;
    let leftSec = 60;
    let clock = '';
    el.onclick = function () {
      if (leftSec < 60) return;
      console.log('---leftSec------:', leftSec)
      el.classList.add('code-sended')
      if (callback && typeof callback === 'function') {
        callback().then(result => {
          leftSec -= 1;
          el.innerHTML = leftSec + 's';
          clock = setInterval(() => {
            if (leftSec < 60 && leftSec > 0) {
              leftSec -= 1;
              el.innerHTML = leftSec + 's';
            } else {
              clearInterval(clock)
              el.innerHTML = '获取验证码';
              leftSec = 60;
            }
          }, 1000)
        }, err => {

        })
      }

    }
  }

})
